package com.example.lj.uav.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.lj.uav.R;

import java.util.List;

import static android.view.View.inflate;


public class SpinnerDropDownAdapter extends ArrayAdapter {
    private String[] array;
    private Context context;
    private int stroke_color;
    private Spinner mSpinner;

    private static final int BLUE_STROKE = 1;
    private static final int WHITE_STROKE = 2;
    private static final int BLACK_STROKE = 3;

    public SpinnerDropDownAdapter(Context context, String[] list, Spinner mSpinner) {
        super(context, R.layout.item_menu_blank, R.id.tv_item_menu_blank_tv, list);
        this.context = context;
        this.array = list;
        this.stroke_color = 1;
        this.mSpinner = mSpinner;
    }

    public SpinnerDropDownAdapter(Context context, String[] list, int stroke_color, Spinner mSpinner) {
        super(context, R.layout.item_menu_blank, R.id.tv_item_menu_blank_tv, list);
        this.context = context;
        this.array = list;
        this.stroke_color = stroke_color;
        this.mSpinner = mSpinner;
    }

    public SpinnerDropDownAdapter(Context context, int resource, int textViewResourceId, String[] list, int stroke_color, Spinner mSpinner) {
        super(context, resource, textViewResourceId, list);
        this.context = context;
        this.array = list;
        this.stroke_color = stroke_color;
        this.mSpinner = mSpinner;
    }

    class ViewHolder {
        TextView tv;
    }

    public String[] getArray() {
        return array;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
        ViewHolder holder;
        holder = new ViewHolder();
        view = LayoutInflater.from(context).inflate(R.layout.item_menu_blank, null);
        holder.tv = view.findViewById(R.id.tv_item_menu_blank_tv);
        switch (stroke_color) {
            case BLUE_STROKE:
                holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_blue_strock);
                holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                break;
            case WHITE_STROKE:
                holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_white_strock);
                holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                break;
            case BLACK_STROKE:
                holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_black_strock);
                holder.tv.setTextColor(context.getResources().getColor(R.color.black_dark_transparent));
                break;
        }
        holder.tv.setText(array[position]);
        return view;
    }

    @Override
    public View getDropDownView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (position == 0) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_menu_blank, null);
            holder.tv = view.findViewById(R.id.tv_item_menu_blank_tv);
            switch (stroke_color) {
                case BLUE_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_blue_stroke_no_bottom);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    }
                    break;
                case WHITE_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_white_stroke_no_bottom);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    }
                    break;
                case BLACK_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_black_stroke_no_bottom);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    }
                    break;
            }
            holder.tv.setText(array[position]);
        } else if (position == array.length - 1) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_menu_blank, null);
            holder.tv = view.findViewById(R.id.tv_item_menu_blank_tv);
            switch (stroke_color) {
                case BLUE_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_blue_stroke_no_top);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    }
                    break;
                case WHITE_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_white_stroke_no_top);
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_white_stroke_no_bottom);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    }
                    break;
                case BLACK_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_black_stroke_no_top);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    }
                    break;
            }
            holder.tv.setText(array[position]);
        } else {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_menu_blank, null);
            holder.tv = view.findViewById(R.id.tv_item_menu_blank_tv);
            switch (stroke_color) {
                case BLUE_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_blue_stroke_side);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    }
                    break;
                case WHITE_STROKE:
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_white_stroke_side);
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    }
                    break;
                case BLACK_STROKE:
                    if (mSpinner.getSelectedItemPosition() == position) {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.dodgerblue));
                    } else {
                        holder.tv.setTextColor(context.getResources().getColor(R.color.white));
                    }
                    holder.tv.setBackgroundResource(R.drawable.shape_line_info_menu_blank_black_stroke_side);
                    break;
            }
//            if (view == null){
//                holder = new ViewHolder();
//
//                view.setTag(holder);
//            }else{
//                holder = (ViewHolder) view.getTag();
//            }
            holder.tv.setText(array[position]);
        }
        return view;
    }

    @Override
    public void setDropDownViewResource(int resource) {
        super.setDropDownViewResource(resource);
    }
}

